//
//  LLMallViewModel.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLMallViewModel.h"
#import "LLMenuItemDataViewModel.h"
#import "LLGoodsBasicViewModel.h"
#import "LLMenuItem.h"
#import "LLMenuItemData.h"
#import "LLGoodsBasic.h"

#define MODULE_TYPE_HOT_ACTIVITY           @"hot_activity"
#define MODULE_TYPE_ALL_CATALOG            @"all_catalog"
#define MODULE_TYPE_RECOMMEND_BRAND        @"recommend_brand"
#define MODULE_TYPE_DAYTOP                 @"daytop"

#define MODULE_TYPE_LIST                    @[MODULE_TYPE_HOT_ACTIVITY, MODULE_TYPE_ALL_CATALOG, MODULE_TYPE_RECOMMEND_BRAND, MODULE_TYPE_DAYTOP]

@interface LLMallViewModel ()
@property(nonatomic, strong, readwrite) NSArray *dataSource;

@property(nonatomic, strong, readwrite) LLMenuItemDataCollectionViewModel *hotActivityDataSource;
@property(nonatomic, strong, readwrite) LLMenuItemDataCollectionViewModel *catalogDataSource;
@property(nonatomic, strong, readwrite) LLMenuItemDataCollectionViewModel *brandDataSource;
@property(nonatomic, strong, readwrite) NSArray *dayTopDataSource;

@property(nonatomic, strong, readwrite) RACCommand *loadMenusCommand;
@property(nonatomic, assign, readwrite) BOOL isLoading;
@end

@implementation LLMallViewModel

- (void)initialize {
    @weakify(self)

    self.hotActivityDataSource = [[LLMenuItemDataCollectionViewModel alloc] init];
    self.catalogDataSource = [[LLMenuItemDataCollectionViewModel alloc] init];
    self.brandDataSource = [[LLMenuItemDataCollectionViewModel alloc] init];
    self.dayTopDataSource = @[];

    self.loadMenusCommand =
            [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
                @strongify(self)
                self.isLoading = YES;

                void (^nextBlock)(NSString *, LLMenuItemDataBag *, LLMenuItem *)
                        = ^(NSString *moduleType, LLMenuItemDataBag *bag, LLMenuItem *item) {
                            @strongify(self)
                            NSArray *array =
                                    [[[bag menuItemDatas].rac_sequence.signal
                                            map:^id(LLMenuItemData *menuItemData) {
                                                LLMenuItemDataViewModel *viewModel = [[LLMenuItemDataViewModel alloc] init];
                                                viewModel.imageUrl = menuItemData.imgUrl;
                                                viewModel.title = menuItemData.name;
                                                return viewModel;
                                            }] toArray];
                            LLMenuItemDataCollectionViewModel *collectionViewModel = [[LLMenuItemDataCollectionViewModel alloc] init];
                            collectionViewModel.itemList = array;
                            collectionViewModel.title = item.name;
                            collectionViewModel.identifier = moduleType;
                            if ([moduleType isEqualToString:MODULE_TYPE_HOT_ACTIVITY]) {
                                self.hotActivityDataSource = collectionViewModel;
                            } else if ([moduleType isEqualToString:MODULE_TYPE_ALL_CATALOG]) {
                                self.catalogDataSource = collectionViewModel;
                            } else {
                                NSMutableArray *mArray = [NSMutableArray arrayWithArray:array];
                                if (mArray.count > 7) {
                                    [mArray removeObjectsInRange:NSMakeRange(7, mArray.count - 7)];
                                }
                                LLMenuItemDataViewModel *moreData = [[LLMenuItemDataViewModel alloc] init];
                                moreData.isRemoteData = YES;
                                moreData.title = @"更多品牌";
                                [mArray addObject:moreData];
                                [mArray enumerateObjectsUsingBlock:^(LLMenuItemDataViewModel *vm, NSUInteger idx, BOOL *_Nonnull stop) {
                                    vm.showTopLine = YES;
                                    vm.showRightLine = YES;
                                    if ((idx + 1) % 4 == 0) {
                                        vm.showRightLine = NO;
                                    }
                                }];
                                collectionViewModel.itemList = mArray;
                                self.brandDataSource = collectionViewModel;
                            }
                        };

                void (^goodsBasicNextBlock)(LLGoodsBasicBag *, LLMenuItem *)
                        = ^(LLGoodsBasicBag *bag, LLMenuItem *item) {
                            @strongify(self)
                            self.dayTopDataSource =
                                    [[bag.goodsBasicItems.rac_sequence.signal
                                            map:^id(LLGoodsBasic *goods) {
                                                LLGoodsBasicViewModel *vm = [[LLGoodsBasicViewModel alloc] init];
                                                vm.goodsBasic = goods;
                                                return vm;
                                            }] toArray];
                        };

                NSDictionary *params = @{@"type" : @"shangcheng"};
                return [[[[[[LLHTTPSessionManager shareManager]
                        request:URL_CHANNEL_AC_LIST params:params resultClass:LLMenuItemBag.class]
                        flattenMap:^RACStream *(LLMenuItemBag *menuItemBag) {
                            return menuItemBag.menuItems.rac_sequence.signal;
                        }]
                        // 转化为对四个模块的数据的请求
                        flattenMap:^RACStream *(LLMenuItem *item) {
                            if ([MODULE_TYPE_LIST containsObject:item.identifier]) {
                                NSDictionary *params = @{@"type" : item.identifier};
                                RACSignal *signal = nil;
                                if ([MODULE_TYPE_DAYTOP isEqualToString:item.identifier]) {
                                    params = @{@"sidx" : @"weight", @"sord" : @"desc"};
                                    signal = [[LLHTTPSessionManager shareManager]
                                            request:URL_GOODS_SEARCH
                                             params:params
                                        resultClass:[LLGoodsBasicBag class]];
                                } else {
                                    params = @{@"type" : item.identifier};
                                    signal = [[LLHTTPSessionManager shareManager]
                                            request:URL_CHANNEL_AC_ACTIVITY_LIST
                                             params:params
                                        resultClass:[LLMenuItemDataBag class]];
                                }
                                return [RACSignal combineLatest:@[
                                        [RACSignal return:item.identifier],
                                        signal,
                                        [RACSignal return:item]
                                ]];
                            }
                            return [RACSignal empty];
                        }]
                        // 对四个请求的结果做处理
                        doNext:^(RACTuple *tupe) {
                            if ([tupe.first isEqualToString:MODULE_TYPE_DAYTOP]) {
                                goodsBasicNextBlock(tupe.second, tupe.third);
                            } else {
                                nextBlock(tupe.first, tupe.second, tupe.third);
                            }
                        }]
                        finally:^{
                            @strongify(self)
                            self.dataSource = @[self.hotActivityDataSource, self.catalogDataSource, self.brandDataSource, self.dayTopDataSource];
                            self.isLoading = NO;
                        }];
            }];

    [self.loadMenusCommand.errors subscribeNext:^(id x) {
        @strongify(self)
        self.isLoading = YES;
    }];
}

@end
